<template>
  <el-row type="flex" align="middle" style="height: 40px; width: 100%">
    <el-col :span="20">
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <el-col>
          <span>{{ treeNode.manager }}</span>
        </el-col>
        <el-col>
          <!-- #2 注册事件 -->
          <!-- 下拉菜单 element -->
          <el-dropdown @command="rightMenuClick">
            <!-- 默认插槽：标题 -->
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 具名插槽：下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="DEPARTS_ADD">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" :command="DEPARTS_DEL">删除部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" :command="DEPARTS_EDIT">编辑部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { delDepartments } from '@/api/departments'
import { DEPARTS_ADD, DEPARTS_DEL, DEPARTS_EDIT } from '@/constants'
export default {
  name: 'TreeTools',
  props: {
    // treeNode 就是那一行的数据
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      DEPARTS_ADD, DEPARTS_DEL, DEPARTS_EDIT
    }
  },
  methods: {
    async rightMenuClick(tag) {
      if (tag === DEPARTS_ADD) {
        // #3 添加，触发父亲的自定义事件
        this.$emit('addDepts', this.treeNode)
      }
      if (tag === DEPARTS_DEL) {
        try {
          await this.$confirm('此操作将永久删除, 是否继续?', '提示', {
            type: 'warning'
          })
          await delDepartments(this.treeNode.id)
          this.$emit('delDepts')
        } catch (e) {
          if (e === 'cancel') {
            // 取消就会走这儿
            this.$message.info('已取消删除')
          }
        }
      }
      if (tag === DEPARTS_EDIT) {
        // !#1 编辑
        this.$emit('editDepts', this.treeNode)
      }
    }
  }
}
</script>
